package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_discover_spec;

import cn.com.api.service.ApiU_discover_specService;

import cn.com.util.*;


public class U_discover_specBaseController {

	private static Logger logger = Logger.getLogger(U_discover_specBaseController.class);
	
	@Autowired
	private ApiU_discover_specService apiU_discover_specService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_discover_spec-list")
	public ModelAndView u_discover_spec_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_discover_spec_list.jsp");
		
		List<ApiU_discover_spec> u_discover_specs = apiU_discover_specService.findU_discover_specs();
		mv.addObject("u_discover_specs", u_discover_specs);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_discover_spec-add-pre")
	public ModelAndView u_discover_spec_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_discover_spec_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_discover_spec u_discover_spec = apiU_discover_specService.findU_discover_specById(Integer.valueOf(id));
			mv.addObject("u_discover_spec",u_discover_spec);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_discover_spec-add")
	public void u_discover_spec_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String conn_id = request.getParameter("conn_id");
		String price_name = request.getParameter("price_name");
		String price = request.getParameter("price");
		String limit = request.getParameter("limit");
		String sort = request.getParameter("sort");
		String type = request.getParameter("type");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiU_discover_spec u_discover_spec = new ApiU_discover_spec();
            /*
		    u_discover_spec.setId(id);
		    u_discover_spec.setConn_id(conn_id);
		    u_discover_spec.setPrice_name(price_name);
		    u_discover_spec.setPrice(price);
		    u_discover_spec.setLimit(limit);
		    u_discover_spec.setSort(sort);
		    u_discover_spec.setType(type);
	        */
			apiU_discover_specService.insertU_discover_spec(u_discover_spec);
		}
		//edit
		else{
			ApiU_discover_spec u_discover_spec = apiU_discover_specService.findU_discover_specById(Integer.valueOf(id));
	        /*
		    u_discover_spec.setId(id);
		    u_discover_spec.setConn_id(conn_id);
		    u_discover_spec.setPrice_name(price_name);
		    u_discover_spec.setPrice(price);
		    u_discover_spec.setLimit(limit);
		    u_discover_spec.setSort(sort);
		    u_discover_spec.setType(type);
			*/
			apiU_discover_specService.updateU_discover_spec(u_discover_spec);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_discover_spec-delete")
	public void u_discover_spec_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_discover_specService.deleteU_discover_spec(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



